package com.school.model.dao;

// default package
// Generated Sep 24, 2013 9:15:18 AM by Hibernate Tools 3.4.0.CR1

import javax.ejb.Stateless;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.school.model.SchoolSessionFactory;
import com.school.model.entity.Schueler;

/**
 * Home object for domain model class Schueler.
 * 
 * @see .Schueler
 * @author Hibernate Tools
 */
@Stateless
public class SchuelerHome {

	private static final Log log = LogFactory.getLog(SchuelerHome.class);

	private SessionFactory entityManager;

	public SchuelerHome() {
		entityManager = SchoolSessionFactory.getInstance();
	}

	public void persist(Schueler transientInstance) {
		log.debug("persisting Schueler instance");
		Session session = null;
		Transaction transaction = null;
		try {
			session = entityManager.openSession();
			transaction = session.beginTransaction();
			session.save(transientInstance);
			transaction.commit();
			session.close();
			log.debug("persist of Schueler " + transientInstance.getName()
					+ " successful");
		} catch (HibernateException re) {
			log.error("persist of Schueler failed", re);
			throw re;
		} finally {
			if (transaction != null && transaction.isActive()) {
				transaction.rollback();
			}
			if (session != null && session.isOpen())
				session.close();
		}
	}

	public void remove(Schueler persistentInstance) {
		log.debug("removing Schüler instance");
		Session session = null;
		Transaction transaction = null;
		try {
			session = entityManager.openSession();
			transaction = session.beginTransaction();
			session.delete(persistentInstance);
			transaction.commit();
			session.close();
			log.debug("removing Schüler " + persistentInstance.getName()
					+ " successful");
		} catch (HibernateException re) {
			log.error("removing Schüler failed", re);
			throw re;
		} finally {
			if (transaction != null && transaction.isActive()) {
				transaction.rollback();
			}
			if (session != null && session.isOpen())
				session.close();
		}
	}

	public Schueler merge(Schueler detachedInstance) {
		log.debug("merging Schueler instance");
		Session session = null;
		Transaction transaction = null;
		Schueler mergedSchueler = null;
		try {
			session = entityManager.openSession();
			transaction = session.beginTransaction();
			mergedSchueler = (Schueler) session.merge(detachedInstance);
			transaction.commit();
			session.close();
			log.debug("merge successful. Following Schueler merged: "
					+ mergedSchueler.getName());
			return mergedSchueler;
		} catch (HibernateException re) {
			log.error("merge failed", re);
			throw re;
		} finally {
			if (transaction != null && transaction.isActive()) {
				transaction.rollback();
			}
			if (session != null && session.isOpen())
				session.close();
		}
	}

	public Schueler findById(int id) {
		log.debug("getting Schueler instance with id: " + id);
		Session session = null;
		Transaction transaction = null;
		Schueler schuelerById = null;
		try {
			session = entityManager.openSession();
			transaction = session.beginTransaction();
			schuelerById = (Schueler) session
					.createQuery("from Schueler as f where f.id = :id")
					.setInteger("id", id).list().get(0);
			transaction.commit();
			session.close();
			log.debug("find by id successful. Following Schueler found "
					+ schuelerById.getName());
			return schuelerById;
		} catch (HibernateException re) {
			log.error("find by id failed", re);
			throw re;
		} finally {
			if (transaction != null && transaction.isActive()) {
				transaction.rollback();
			}
			if (session != null && session.isOpen())
				session.close();
		}
	}
}
